package com.lucas.service.Impl;

import com.lucas.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @program: spring_cloud_lagou_homework
 * @author: lucasLu
 **/
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public boolean sendSimpleMail(String to, String subject, String content) {
        return sendMail(to,subject,content,false);
    }

    private boolean sendMail(String to, String subject, String content, boolean html) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(mimeMessage,true);
            //邮件发送人
            messageHelper.setFrom("252458848@qq.com");
            messageHelper.setTo(to);
            mimeMessage.setSubject(subject);
            messageHelper.setText(content,html);
            mailSender.send(mimeMessage);

            return true;
        }catch (MessagingException e){
            return false;
        }
    }

    @Override
    public boolean sendHtmlMail(String to, String subject, String content) {
        return sendMail(to,subject,content,true);
    }
}
